/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ServiceController;

import com.Service.BO.OrganizationBO;
import com.Service.DAO.SuperDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.inject.Stereotype;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author olugbokikisemiu
 */
public class SaveOrganController extends HttpServlet {
    
    SuperDAO organ;
    OrganizationBO org;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        organ = new SuperDAO();
        org = new OrganizationBO();
        //List<String> test = new ArrayList<String>();

        try {
            String orgname = request.getParameter("orgname");
            String shortdesc = request.getParameter("shortDesc");
            String lead = request.getParameter("leadcontact");
            String add1 = request.getParameter("Add1");
            String add2 = request.getParameter("add2");
            String add3 = request.getParameter("add3");
            String post = request.getParameter("postcode");
            String city = request.getParameter("city");
            String county = request.getParameter("county");
            int nation = Integer.parseInt(request.getParameter("country"));
            String pref = request.getParameter("prefer");
            String exp = request.getParameter("express");
            String Type = request.getParameter("type");
            String sic = request.getParameter("sic");
            String full = request.getParameter("fulldesc");
            int fone = Integer.parseInt(request.getParameter("number"));
            int fax = Integer.parseInt(request.getParameter("fax"));
            String email = request.getParameter("email");
            String web = request.getParameter("web");
            int charity = Integer.parseInt(request.getParameter("charity"));
            int com = Integer.parseInt(request.getParameter("company"));
            String[] spec = request.getParameterValues("specialism");
            String[] disable = request.getParameterValues("disability");
            String[] barrier = request.getParameterValues("barrier");
            String[] benefit = request.getParameterValues("benet");
            String[] personel = request.getParameterValues("person");
            String[] ethnic = request.getParameterValues("ethnic");
            String[] accred = request.getParameterValues("acreed");
            String[] program = request.getParameterValues("program");
            String[] serve = request.getParameterValues("server");
            
            
            
            org.setName(orgname);
            org.setDesc(shortdesc);
            org.setLead(lead);
            org.setAddline1(add1);
            org.setAddline2(add2);
            org.setAddline3(add3);
            org.setPostcode(post);
            org.setCity(city);
            org.setCounty(county);
            org.getCountry().setID(nation);
            org.setPreOrgan(pref);
            org.setExInterest(exp);
            org.setTypeOfBiz(Type);
            org.setSicCode(sic);
            org.setFullDesc(full);
            org.setFone(fone);
            org.setFax(fax);
            org.setEmail(email);
            org.setWebAdd(web);
            org.setCharity(charity);
            org.setComNumber(com);
            
            if (request.getParameter("IsActive") != null) {
                org.setIsActive("Yes");
            } else {
                org.setIsActive("No");
            }
            
            
            
            for (int i = 0; i < spec.length; i++) {
                
                if (spec[i].equals(spec[i])) {
                    
                    org.setOrganSpec(spec[i] + "," + org.getOrganSpec());
                }
                
            }
            for (int a = 0; a < disable.length; a++) {
                if (disable[a].equals(disable[a])) {
                    org.setSDisCap(disable[a] + "," + org.getSDisCap());
                }
            }
            for (int b = 0; b < barrier.length; b++) {
                if (barrier[b].equals(barrier[b])) {
                    org.setSBarCap(barrier[b] + "," + org.getSBarCap());
                }
            }
            for (int c = 0; c < benefit.length; c++) {
                if (benefit[c].equals(benefit[c])) {
                    org.setSBenCap(benefit[c] + "," + org.getSBenCap());
                }
            }
            for (int d = 0; d < personel.length; d++) {
                if (personel[d].equals(personel[d])) {
                    org.setSPCcap(personel[d] + "," + org.getSPCcap());
                }
            }
            for (int e = 0; e < ethnic.length; e++) {
                if (ethnic[e].equals(ethnic[e])) {
                    org.setSetCap(ethnic[e] + "," + org.getSetCap());
                }
            }
            for (int f = 0; f < accred.length; f++) {
                if (accred[f].equals(accred[f])) {
                    org.setAccred(accred[f] + "," + org.getAccred());
                }
            }
            for (int g = 0; g < program.length; g++) {
                if (program[g].equals(program[g])) {
                    org.setEOIProg(program[g] + " ," + org.getEOIProg());
                }
            }
            for (int h = 0; h < serve.length; h++) {
                if (serve[h].equals(serve[h])) {
                    org.setEOIService(serve[h] + "," + org.getEOIService());
                }
            }
            
            organ.addOrganization(org);
            
            RequestDispatcher req = request.getRequestDispatcher("ListOrganController");
            req.forward(request, response);
           
            
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
